#ifndef _MIX_THREAD_MUTEX_H_
#define _MIX_THREAD_MUTEX_H_

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#else
#include <pthread.h>
#endif

namespace mix
{

class Mutex
{
public:
	Mutex();
	~Mutex();

	void lock();
	void unlock();

	//! 尝试加锁,加锁成功返回true,加锁失败返回false
	bool tryLock();

    bool tryLock( long msec);

	void* getNative();
public:
	class Guard
	{
		Mutex* _mutex;
        Guard( Guard&); 
        Guard& operator= ( const Guard&);
	public:
		explicit Guard( Mutex& m) : _mutex( &m) {
			_mutex->lock();
		}
		~Guard() {
			_mutex->unlock();
		}
	};

private:
    Mutex( const Mutex&) {}
    Mutex& operator= ( const Mutex&) {}
#if defined(_WIN32) || defined(_WIN64)
    ::CRITICAL_SECTION _mutex;
#else
	::pthread_mutex_t _mutex;
#endif
};

}	// namespace mix

#endif	// _MIX_THREAD_MUTEX_H_
